package com.zy.Conterller;


import com.zy.pojo.Staff;
import com.zy.service.StaffService;
import com.zy.vo.ManagementVo;
import com.zy.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sal/staff")
public class StaffCount {
    @Resource
    private StaffService staffService;

    @GetMapping("/list/{pageSize}/{pageNo}")
  @RequiresPermissions("staff:list")
    public ResultVo getUserList(@PathVariable Integer pageSize, @PathVariable Integer pageNo, ManagementVo managementVo) {
        return ResultVo.success(staffService.getPage(pageNo,pageSize,managementVo));
    }
    @PostMapping("add")
  @RequiresPermissions("staff:add")
    public ResultVo getRoleadd(@RequestBody Staff staff) {
        try{
            staff.setCreationDate(new Date());
            staffService.add(staff);
            return ResultVo.success("员工增加成功😊");
        }catch (Exception e){
            return  ResultVo.fail("员工添加失败",e);
        }
    }

    @PostMapping("update")
   @RequiresPermissions("staff:update")
    public ResultVo getRoleupdate(@RequestBody Staff staff){
        try{
            staff.setModifyDate(new Date());
            staffService.update(staff);
            return ResultVo.success("员工修改成功😊");
        }catch (Exception e){
            return ResultVo.fail("员工修改失败",e);
        }
    }
    @GetMapping("del/{id}")
    @RequiresPermissions("staff:del")
    public ResultVo getUserdel(@PathVariable String id){
        try{
            staffService.del(Integer.parseInt(id));
            return  ResultVo.success("删除员工成功😊");
        }catch (Exception e){
            return ResultVo.fail("删除员工失败");
        }
    }
    @GetMapping("err/{id}")
  @RequiresPermissions("staff:list")
    public ResultVo geterr(@PathVariable String id){
        try{
            staffService.err(Integer.parseInt(id));
            return  ResultVo.success("查询所有员工成功");
        }catch (Exception e){
            return ResultVo.fail("查询所有员工失败");
        }
    }
    @PostMapping("/upload")
    @RequiresPermissions("staff:upload")
    public ResultVo upload(MultipartFile attach, HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        String realPath =servletContext.getRealPath("/images");//获取项目根路径
        System.out.println(realPath+"=============++++++++++++++");
        File floader =new File(realPath);
        if(!floader.exists()){
            floader.mkdir();//如果不存在这个文件夹就创建上
        }
        String original =attach.getOriginalFilename();//文件原始名字
        System.out.println("原始名字:"+original);
        int index =original.lastIndexOf(".");
        String suffix =original.substring(index);//.jpg
        String newFileName= UUID.randomUUID().toString().replace("-","")+suffix;//文件新名字
        File file =new File(realPath,newFileName);
        try {
            attach.transferTo(file);//文件上传
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map =new HashMap<>();
        map.put("attachPath","images/"+newFileName);
        return ResultVo.success(map);
    }
    @PostMapping("/uploadAttach")
    public ResultVo uploadAttach(@RequestBody Staff staff){
        try{
            staffService.updateById(staff);
            return ResultVo.success("上传工作照到数据库成功");
        }catch (RuntimeException e){
            return  ResultVo.fail("上传工作照到数据库失败",e);
        }

    }


}
